package com.taxi.driver.module.web;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.alibaba.fastjson.JSON;
import com.mob.MobSDK;
import com.mob.tools.utils.BitmapHelper;
import com.qianxx.utils.ToastUtil;
import com.qianxx.view.HeadView;
import com.socks.library.KLog;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseWebView;
import com.taxi.driver.data.entity.ShareEntity;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2017/9/20.
 */

public class ShareActivity extends BaseWebView implements PlatformActionListener {

    @BindView(R.id.head_view)
    HeadView mHeadView;

    private String mShareTitle;
    private String mShareText;
    private String mShareUrl;
    private String mShareImg;

    public static void actionStart(Context context, String url) {
        Intent intent = new Intent(context, ShareActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5);
        ButterKnife.bind(this);

        url = getIntent().getStringExtra("url");
        KLog.e(url);

        initWebView();
        loadWebData();
        webView.addJavascriptInterface(this, "share");

        setRefreshTitleListener(new RefreshTitleListener() {
            @Override
            public void onRefreshTitle(String title) {
                mHeadView.setTitle(title);
            }
        });
    }

    @JavascriptInterface
    public void wechat(String json) {
        shareInfo(json);
        initPlatform(Wechat.NAME);
    }

    @JavascriptInterface
    public void friends(String json) {
        shareInfo(json);
        initPlatform(WechatMoments.NAME);
    }

    @JavascriptInterface
    public void qq(String json) {
        shareInfo(json);
        initPlatform(QQ.NAME);
    }

    @JavascriptInterface
    public void spaceQ(String json) {
        shareInfo(json);
        initPlatform(QZone.NAME);
    }

    private void shareInfo(String json) {
        ShareEntity entity;
        entity = JSON.parseObject(json, ShareEntity.class);

        if (entity == null) {
            return;
        }

        mShareTitle = entity.getLinkTitle();
        mShareText = entity.getLinkContent();
        mShareUrl = entity.getLinkUrl();
        mShareImg = entity.getLinkImg();
    }


    private void initPlatform(String platform) {
        MobSDK.init(this);
        Platform.ShareParams sp = new Platform.ShareParams();
        if (!platform.equals(QZone.NAME)) {
            sp.setTitle(mShareTitle);
        }
        sp.setText(mShareText);
        if (platform.equals(Wechat.NAME) || platform.equals(WechatMoments.NAME)
                ) {
            sp.setUrl(mShareUrl);
            sp.setShareType(Platform.SHARE_WEBPAGE);
        }
        if (platform.equals(QQ.NAME) || platform.equals(QZone.NAME)) {
            sp.setTitleUrl(mShareUrl);
        }
        if (platform.equals(QZone.NAME)) {
            sp.setSite(getResources().getString(R.string.app_name));
        }
        if (TextUtils.isEmpty(mShareImg)) {
            sp.setImagePath(changeResource2File(this, R.drawable.share_icon));
        } else {
            sp.setImageUrl(mShareImg);
        }
        Platform plat = ShareSDK.getPlatform(platform);
        plat.setPlatformActionListener(this);
        plat.share(sp);
    }

    private static String changeResource2File(Context context, @DrawableRes int res) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res);
        try {
            return BitmapHelper.saveBitmap(context, bitmap);
        } catch (Throwable throwable) {
            return null;
        }
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        ToastUtil.getInstance().toast("分享成功");
    }

    @Override
    public void onError(Platform patform, int i, Throwable throwable) {
        // 操作失败的处理代码
        String expName = throwable.getClass().getSimpleName();
        Message msg = new Message();
        //判断有没有安装客户端
        if ("WechatClientNotExistException".equals(expName)
                || "WechatTimelineNotSupportedException".equals(expName)
                || "WechatFavoriteNotSupportedException".equals(expName)) {
            msg.obj = "没有安装微信";
        } else if ("QQClientNotExistException".equals(expName)) {
            msg.obj = "没有安装QQ";
        }
        handler.sendMessage(msg);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        ToastUtil.getInstance().toast("分享取消");
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            String content = (String) msg.obj;
            ToastUtil.getInstance().toast(content);
        }
    };
}
